//
//  reportView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/2.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

enum CustomType {
    // 不正当内容
    case share
    // 欺骗
    case report
}

typealias ReportClosure = (NSInteger, ReportView) -> Void

class ReportView: UIView {
    private let topViewHeight: CGFloat = 26
    private let buttonLeftMargin: CGFloat = 29
    private let buttonTopMargin: CGFloat = 15
    private let buttonHeight: CGFloat = 28
    private let buttomImageViewHeight: CGFloat = 39
    private var allButtonHeight: CGFloat = 0
    private var showView: UIView = UIView()
    var titles: [String] = []
    private var reportClosure: ReportClosure?
    private var isFinish = false
    
    /// 创建方法
    ///
    /// - Parameters:
    ///   - contnets: 内容
    ///   - reportClosure: 返回的闭包
    func show(contnets: [String]?, type: CustomType,  reportClosure: @escaping ReportClosure) {
        self.reportClosure = reportClosure
        if let cont = contnets {
            titles = cont
        }
        
        let window = UIApplication.shared.keyWindow
        self.frame = window!.bounds
        self.alpha = 0.3
        self.backgroundColor = ONColor.maskBGColor
        window?.addSubview(self)
        window?.bringSubview(toFront: self)
        UIView.animate(withDuration: TimeInterval(anTIME)) {
            self.alpha = 1
        }
        setMainView()
        switch type {
        case .report:
            setReportView(setTopView(text: "请选择举报原因"))
        case .share:
            setShareView(setTopView(text: "分享到"))
        }
    }
    
    private func setMainView() {
        showView.frame = CGRect(x: 0, y: 175, width: kHeight(r: 242), height: 0)
        let window = UIApplication.shared.keyWindow
        showView.centerX = window!.centerX
        showView.backgroundColor = UIColor.white
        showView.layer.cornerRadius = CORNERRADIUS
        showView.layer.masksToBounds = true
        self.addSubview(showView)
    }
    
    private func setTopView(text: String) -> UIView {
        let topView = UIView(frame: CGRect(x: 0, y: 0, width: showView.width, height: kHeight(r: topViewHeight)))
        topView.backgroundColor = UIColor(hex: 0xC7C7C7)
        showView.addSubview(topView)
        
        let topLabel = UILabel()
        topLabel.text = text
        topLabel.textColor = UIColor.black
        topLabel.font = UIFont.systemFont(ofSize: 15)
        topLabel.sizeToFit()
        topLabel.center = CGPoint(x: topView.width / 2, y: topView.height / 2)
        topView.addSubview(topLabel)
        return topView
    }
    
    private func setReportView(_ topView: UIView) {
        
        for (index, title) in titles.enumerated() {
            let button = UIButton(type: .custom)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            button.setTitle(title, for: .normal)
            button.setTitleColor(ONColor.reportTextColor, for: .normal)
            button.sizeToFit()
            
            button.frame = CGRect(x: buttonLeftMargin, y: topView.height + 13 + CGFloat(index) * buttonTopMargin + CGFloat(index) * buttonHeight, width: button.width, height: buttonHeight)
            button.tag = index
            button.addTarget( self, action: #selector(tapReport(_:)), for: .touchUpInside)
            showView.addSubview(button)
        }
        
        allButtonHeight = CGFloat(titles.count) * buttonHeight + CGFloat(titles.count + 1) * buttonTopMargin
        let imageView = setBottomImageView(allButtonHeight: allButtonHeight, topView: topView)
        showView.height = topView.height + allButtonHeight + imageView.height
    }
    
    private func setShareView(_ topView: UIView) {
        let images = [UIImage(named: "weChat_icon")!, UIImage(named: "weibo_icon")!, UIImage(named: "qq_icon")!, UIImage(named: "circle_of_friends")!]
        let buttonSideMargin: CGFloat = 16
        let buttonTopMargin: CGFloat = 30
        let buttonMargin: CGFloat = ((showView.width - buttonSideMargin * 2.0) - (CGFloat(images.count) * images[0].size.width)) / CGFloat(images.count - 1)
        for (index, image) in images.enumerated() {
            let button = UIButton(type: .custom)
            button.setImage(image, for: .normal)
            button.adjustsImageWhenHighlighted = false
            button.frame = CGRect(x: buttonSideMargin + CGFloat(index) * image.size.width + CGFloat(index) * buttonMargin, y: buttonTopMargin + topView.height, width: image.size.width, height: image.size.height)
            button.tag = index
            button.addTarget(self, action: #selector(tapReport(_ :)), for: .touchUpInside)
            showView.addSubview(button)
        }
        
        let labelTopMargin: CGFloat = 32
        
        let allButtonHeight = buttonTopMargin + images[0].size.height + labelTopMargin * 2 + 20
        
        let imageView = setBottomImageView(allButtonHeight: allButtonHeight, topView: topView)
        
        showView.height = topView.height + allButtonHeight + imageView.height
    }
    
    private func setBottomImageView(allButtonHeight: CGFloat, topView: UIView) -> UIImageView {
        
        let imageView = UIImageView(image: UIImage(named: "common_prohibit"))
        imageView.contentMode = .center
        imageView.frame = CGRect(x: 0, y: topView.height + allButtonHeight, width: showView.width, height: kHeight(r: buttomImageViewHeight))
        imageView.isUserInteractionEnabled = true
        imageView.backgroundColor = ONColor.inputBoxBGColor
        let tap = UITapGestureRecognizer(target: self, action: #selector(cancelTap))
        imageView.addGestureRecognizer(tap)
        showView.addSubview(imageView)
        return imageView
    }
    
    @objc func cancelTap() {
        hide()
    }
    
    func hideAndShow() {
        var imageView = UIImageView()
//        for view in showView.subviews {
//            if !view.isKind(of: UIImageView.self) {
//                view.removeFromSuperview()
//            } else {
//                imageView = view as! UIImageView
//            }
//        }
        showView.subviews.map { (view) -> Void in
            if !view.isKind(of: UIImageView.self) {
                view.removeFromSuperview()
            } else {
                imageView = view as! UIImageView
            }
        }
        
        UIView.animate(withDuration: TimeInterval(anTIME), animations: {
            self.showView.height = 130
            imageView.bottom = self.showView.height
        }) { (make) in
            let label = UILabel()
            label.text = "举报成功"
            label.font = UIFont.systemFont(ofSize: 18)
            label.sizeToFit()
            label.frame.origin.y = 32
            self.showView.addSubview(label)
            label.centerX = self.showView.width / 2
        }
        
        isFinish = true
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let view = touches.first?.view {
            if view.isEqual(self) {
                hide()
            }
        }
    }
    
    func hide() {
        UIView.animate(withDuration: TimeInterval(anTIME), animations: {
            self.alpha = 0.3
        }) { (make) in
            self.removeFromSuperview()
        }
    }
    
    @objc func tapReport(_ btn: UIButton) {
        if isFinish {
            hide()
        } else {
            self.reportClosure?(btn.tag, self)
        }
    }
}
